package com.maserb.monitor.web.schoolConduct;

import com.maserb.monitor.common.AjaxResult;
import com.maserb.monitor.common.AjaxUtils;
import com.maserb.monitor.common.Func_T;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolConduct.ApplicationRecommendEntity;
import com.maserb.monitor.entity.schoolStudent.SchoolStudentParentEntity;
import com.maserb.monitor.model.JwFileInfoModel;
import com.maserb.monitor.model.SchoolTeacherModel;
import com.maserb.monitor.model.StudentParentModel;
import com.maserb.monitor.service.JwReport.JwFileInfoService;
import com.maserb.monitor.service.author_manage.UserService;
import com.maserb.monitor.service.common.FileService;
import com.maserb.monitor.service.schoolConduct.ApplicationRecommendService;
import com.maserb.monitor.service.schoolStudent.SchoolStudentParentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.thymeleaf.util.StringUtils;

import java.util.List;

/**
 * 用户信息手机端
 */
@RestController
@RequestMapping("/schoolUserDataApp")
public class SchoolUserDataAppController {

    @Autowired
    private ApplicationRecommendService service;

    @Autowired
    private SchoolStudentParentService parentService;

    @Autowired
    private UserService userService;

    @Autowired
    FileService fileService;

    @Autowired
    JwFileInfoService jwFileInfoService;


    /**
     * 根据家长id获取家长信息
     * @param id
     * @return
     */
    @RequestMapping("/getParentById")
    @ResponseBody
    public AjaxResult<StudentParentModel> getParentById(String id) {
        return AjaxUtils.process(new Func_T<StudentParentModel>() {
            @Override
            public StudentParentModel invoke() throws Exception {
                SchoolStudentParentEntity entity = parentService.findOne(id);
                StudentParentModel model = new StudentParentModel();
                model.setAddress(entity.getAddress());
                model.setIdcard(entity.getIdcard());
                model.setImage(entity.getImage());
                model.setPhoneNumber(entity.getPhoneNumber());
                model.setRealName(entity.getRealName());
                model.setSex(entity.getSex());
                model.setUserName(entity.getUserName());
                return model;
            }
        });
    }

    /**
     * 根据家长id获取老师信息
     * @param id
     * @return
     */
    @RequestMapping("/getTeacherById")
    @ResponseBody
    public AjaxResult<SchoolTeacherModel> getTeacherById(String id) {
        return AjaxUtils.process(new Func_T<SchoolTeacherModel>() {
            @Override
            public SchoolTeacherModel invoke() throws Exception {
                UserEntity entity = userService.findOne(id);
                SchoolTeacherModel model = new SchoolTeacherModel();
                model.setAddress(entity.getAddress());
                model.setIdcard(entity.getIdcard());
                model.setImage(entity.getImage());
                model.setPhoneNumber(entity.getPhoneNumber());
                model.setName(entity.getRealName());
                model.setSex(entity.getSex());
                model.setUserName(entity.getUserName());
                return model;
            }
        });
    }

    //手机端上传图片到后台
    @RequestMapping(value = "/uploadPhoto",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult<String> uploadPhoto(MultipartHttpServletRequest muiltRequest) {
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                String fileName = muiltRequest.getFileNames().next(); //得到文件名（注意。是content-type 中的name="file1"，而不是真正的文件名）
                MultipartFile file = muiltRequest.getFile(fileName);   //得到该文件
                //图片
                String path = jwFileInfoService.uploadPhoto(file);
                if(!StringUtils.isEmptyOrWhitespace(path)){
                    return path;
                }else{
                    throw new Exception("请上传图片");
                }
            }
        });
    }

    /**
     * 修改家长资料
     * @param userId
     * @param image
     * @param address
     * @param realName
     * @return
     */
    @RequestMapping("/updateParentById")
    @ResponseBody
    public AjaxResult<Boolean> updateParentById(String userId, String image,String address,String realName) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                if(!StringUtils.isEmptyOrWhitespace(userId)){
                    SchoolStudentParentEntity parent = parentService.findOne(userId);
                    if(null!=parent && !"".equals(parent.getId())){
                        if(!StringUtils.isEmptyOrWhitespace(image)){
                            parent.setImage(image);
                        }else{
                            throw new Exception("请上传图片!");
                        }
                        if(!StringUtils.isEmptyOrWhitespace(address)){
                            parent.setAddress(address);
                        }
                        if(!StringUtils.isEmptyOrWhitespace(realName)){
                            parent.setRealName(realName);
                        }
                        parentService.save(parent);
                    }else{
                        throw new Exception("用户不正确");
                    }
                }else{
                    throw new Exception("用户不正确!");
                }
                return true;
            }
        });
    }

    /**
     * 修改老师资料
     * @param userId
     * @param image
     * @param address
     * @param realName
     * @return
     */
    @RequestMapping("/updateTeacherById")
    @ResponseBody
    public AjaxResult<Boolean> updateTeacherById(String userId, String image,String address,String realName) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                if(!StringUtils.isEmptyOrWhitespace(userId)){
                    UserEntity user = userService.findOne(userId);
                    if(null!=user && !"".equals(user.getId())){
                        if(!StringUtils.isEmptyOrWhitespace(image)){
                            user.setImage(image);
                        }else{
                            throw new Exception("请上传图片!");
                        }
                        if(!StringUtils.isEmptyOrWhitespace(address)){
                            user.setAddress(address);
                        }
                        if(!StringUtils.isEmptyOrWhitespace(realName)){
                            user.setRealName(realName);
                        }
                        userService.save(user);
                    }else{
                        throw new Exception("用户不正确");
                    }
                }else{
                    throw new Exception("用户不正确!");
                }
                return true;
            }
        });
    }

    /**
     * 修改用户头像
     * type 1，教师修改头像    2，家长修改头像
     * @param muiltRequest
     * @param userId
     * @param type
     * @return
     */
    @RequestMapping(value = "/uploadUserPhoto",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult<String> uploadUserPhoto(MultipartHttpServletRequest muiltRequest,String userId,String type) {
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                if(!StringUtils.isEmptyOrWhitespace(userId) && !StringUtils.isEmptyOrWhitespace(type) && null!=muiltRequest){
                    String fileName = muiltRequest.getFileNames().next(); //得到文件名（注意。是content-type 中的name="file1"，而不是真正的文件名）
                    MultipartFile file = muiltRequest.getFile(fileName);   //得到该文件
                    //图片
                    String path = jwFileInfoService.uploadPhoto(file);
                    if(StringUtils.isEmptyOrWhitespace(path)){
                        throw new Exception("请上传图片");
                    }
                    if("1".equals(type)){
                        UserEntity user = userService.findOne(userId);
                        if(null!=user){
                            user.setImage(path);
                            userService.save(user);
                            return path;
                        }else{
                            throw new Exception("教师不正确");
                        }
                    }else if("2".equals(type)){
                        SchoolStudentParentEntity parentEntity =parentService.findOne(userId);
                        if(null!=parentEntity){
                            parentEntity.setImage(path);
                            parentService.save(parentEntity);
                            return path;
                        }
                        else{
                            throw new Exception("家长不正确");
                        }
                    }else{
                        throw new Exception("请传入正确用户类型");
                    }
                }else{
                    throw new Exception("请传入正确数据");
                }
            }
        });
    }



}
